<template>
  <!-- 分配客户经理 -->
  <el-dialog destroy-on-close :modal-append-to-body="false" @close="fpkhjlCancel('fpkhjlRuleForm')"
    :close-on-click-modal="false" title="分配客户经理" :visible.sync="fpkhjlVisible" width="500px" label-position="left">
    <el-form :model="fpkhjlRuleForm" :rules="fpkhjlRules" ref="fpkhjlRuleForm" label-position="top">
      <el-form-item label="客户经理" prop="account_manager_id">
        <el-select class="input-box" v-model="fpkhjlRuleForm.account_manager_id" placeholder="请选择" filterable clearable>
          <el-option v-for="item in accountManagerList" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input :rows="4" class="input-box" v-model="fpkhjlRuleForm.remark" type="textarea"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button @click="fpkhjlCancel('fpkhjlRuleForm')">关闭</el-button>
      <el-button type="primary" @click="fpkhjlSubmit('fpkhjlRuleForm')" :disabled="diaLoading" :loading="diaLoading">确
        定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import projectApi from "@/api/projectApi";
import companyApi from "@/api/companyApi";
import adminApi from "@/api/adminApi";
import { getUserInfo } from "@/utils/auth";
import foremanApi from "@/api/foremanApi";
export default {
  name: "",
  props: {
    addDialogVisible: {
      type: Boolean,
      default: false,
      required: false,
    },
    project_id: {
      required: false,
    },
  },
  data() {
    return {
      // 分配客户经理
      fpkhjlVisible: false,
      fpkhjlRuleForm: {
        account_manager_id: "",
        remark: "",
      },
      accountManagerList: [], //客户经理
      fpkhjlRules: {
        account_manager_id: [
          { required: true, message: "请选择客户经理", trigger: "change" },
        ],
      },
      diaLoading: false,
    };
  },
  watch: {
    addDialogVisible(val1, val2) {
      if (this.addDialogVisible) {
        this.get_account_manager();
      }
      this.$nextTick(() => {
        this.fpkhjlVisible = this.addDialogVisible;
      });
    },
  },
  computed: {},
  mounted() { },
  methods: {
    // 分配客户经理
    fpkhjlCancel(formName) {
      this.fpkhjlRuleForm = {
        account_manager_id: "",
        remark: "",
      };
      this.$nextTick(() => {
        this.$refs[formName].clearValidate();
        // this.fpkhjlVisible = false;
        this.$emit("onClose", true);
      });
    },
    fpkhjlSubmit(formName) {
      let fpkhjlRuleForm = this.fpkhjlRuleForm;
      console.log(fpkhjlRuleForm, "分配客户经理参数");
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.diaLoading = true;
          projectApi
            .assignAccountManager({
              account_manager_id: fpkhjlRuleForm.account_manager_id,
              remark: fpkhjlRuleForm.remark,
              project_id: this.project_id,
            })
            .then((res) => {
              console.log(res, "分配客户经理");
              this.diaLoading = false;
              if (res.code == 200) {
                this.$emit("onSuccess", true);
              }
              this.$message({
                type: res.code == 200 ? "success" : "warning",
                message: res.code == 200 ? "处理成功" : res.message,
              });
            })
            .catch((err) => {
              this.diaLoading = false;
              this.$message({
                type: "warning",
                message: err.message,
              });
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    // 客户经理
    get_account_manager() {
      var obj = {
        page: 1,
        num: 9999,
        keyword: "",
        province_id: "",
        city_id: "",
        county_id: "",
        identity: 1,
        position_id_arr: [2],
      };
      adminApi
        .getAdminList(obj)
        .then((res) => {
          console.log(res, "客户经理");
          if (res.code == 200) {
            this.accountManagerList = res.data.list;
          } else {
            this.$message({
              message: res.message,
              type: "warning",
            });
          }
        })
        .catch((err) => { });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "./index.scss";
</style>